package cf.huzpsb.bcgod;

import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Iterator;

public class TbanAdapt implements Runnable {
    private DatagramSocket socketr;
    private int prt;

    public TbanAdapt(int port) {
        prt = port;
    }

    @Override
    public void run() {
        byte[] bs = new byte[500];
        DatagramPacket p = new DatagramPacket(bs, bs.length);
        try {
            socketr = new DatagramSocket(prt);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                socketr.receive(p);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String st = new String(bs, 0, p.getLength());
            String[] sts = st.split("~");
            if (sts.length == 2) {
                if (sts[0].equalsIgnoreCase("kicky")) {
                    if (!Main.bancfg.contains(sts[1])) {
                        Main.bancfg.set(sts[1], "T");
                        BungeeCord.getInstance().getLogger().info(sts[1] + " is kicked by Tban.");
                    }
                    Iterator var18 = Main.getInstance().getProxy().getPlayers().iterator();
                    while (var18.hasNext()) {
                        ProxiedPlayer player1 = (ProxiedPlayer) var18.next();
                        if (player1.getName().equals(sts[1])) {
                            player1.disconnect(Main.kickr);
                        }
                    }
                    continue;
                }
                int i;
                try {
                    i = Integer.parseInt(sts[1]);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                int nh = 0;
                if (Main.horncfg.contains(sts[0])) {
                    nh = Main.horncfg.getInt(sts[0]);
                }
                nh += i;
                if (nh <= 0) {
                    nh = 0;
                }
                if (nh == 0) {
                    Main.horncfg.set(sts[0], null);
                } else {
                    Main.horncfg.set(sts[0], nh);
                }
                BungeeCord.getInstance().getLogger().info(sts[0] + " now has horn: " + nh);
            } else {
                if (!Main.bancfg.contains(st)) {
                    Main.bancfg.set(st, "T");
                    BungeeCord.getInstance().getLogger().info(st + " is banned by Tban.");
                }
            }
        }
    }
}
